<template>
  <view class="content">
    <view class="tips" v-if="needAuth">
      <text>功能将被受限，点击右方按钮授权 </text>
      <view class="auth_btn" @click="doAuth">获取完整功能</view>
    </view>
    <view class="bk"> </view>

    <view class="user_info">
      <!-- #ifdef MP-ALIPAY -->
      <!-- <button type="primary" size="default" open-type="getAuthorize" scope="userInfo" @getAuthorize="onGetAuthorize" @error="onAuthError">
        支付宝一键登录
      </button> -->
      <!-- #endif -->
      <image class="bk_info" src="../../static/bk_info.png"></image>

      <view class="info_data">
        <image class="avater" :src="avatarUrlLocal ? avatarUrlLocal : '../../static/icon_no_ava.png'"></image>
        <!-- <image class="avater" :src="'../../static/icon_no_ava.png'"></image> -->
        <button class="repair-button" open-type="chooseAvatar" @chooseavatar="onChooseAvatar"></button>

        <view class="info">
          <input type="nickname" v-model="nickNameLocal" class="weui-input" placeholder="请完善昵称信息" @change="nickChange" />
          <text style="font-weight: 600; font-size: 28rpx">{{ userInfo.nickName }}</text>
          <text style="font-size: 24rpx">开始挑选心仪的商品吧~</text>
        </view>
      </view>
    </view>

    <view class="order_all">
      <view class="order_title">
        <view style="font-size: 30rpx; font-weight: 600">我的订单</view>
        <view style="display: flex; align-items: center; height: 100%" @click="goOrderList">
          <text style="font-size: 22rpx; color: #ccc; margin-right: 5rpx; margin-top: 5rpx">全部订单</text>
          <u-icon name="arrow-right" color="#ccc" size="8"></u-icon>
        </view>
      </view>

      <view class="order_status" style="margin-top: 20rpx">
        <view
          v-for="(item, index) in orderStatusItems"
          :key="index"
          @click="goOrderList(item)"
          class="r_status"
          :style="{
            width: 100 / orderStatusItems.length + 'vw',
          }"
        >
          <image :src="item.icon" class="status_icon"></image>
          <text>{{ item.title }}</text>

          <u-badge type="error" max="99" :value="item.num" absolute :offset="[-5, 20]" shape="horn"></u-badge>
        </view>
      </view>

      <view class="menu">
        <view class="menu_item" @click="goAgreement(0)">
          <view class="menu_title">用户协议</view>
          <u-icon name="arrow-right" color="#ccc" size="12"></u-icon>
        </view>
        <view class="menu_item" @click="goAgreement(1)">
          <view class="menu_title">隐私协议</view>
          <u-icon name="arrow-right" color="#ccc" size="12"></u-icon>
        </view>
      </view>
    </view>

    <!-- <auth @authSuccess="authSuccess"></auth> -->
  </view>
</template>

<script setup lang="ts">
import { nextTick, onBeforeMount, onMounted, reactive, ref, watch } from "vue";
import { getOrderData, doAuth, doWxAuth, updateUserNickName } from "@/utils/api";
import auth from "@/components/auth.vue";

const props = defineProps({
  jsonPage: {
    type: Object,
    default: () => {},
  },
});

const orderStatusItems = ref([
  // {
  //   icon: "../../static/icon_daifukuan.png",
  //   title: "待付款",
  //   num: 0,
  //   status: 0,
  // },
  {
    icon: "../../static/icon_daifahuo.png",
    title: "待发货",
    num: 0,
    status: 1,
  },
  {
    icon: "../../static/icon_daishouhuo.png",
    title: "待收货",
    num: 0,
    status: 2,
  },
  {
    icon: "../../static/icon_daipingjia.png",
    title: "已完成",
    num: 0,
    status: 3,
  },
  {
    icon: "../../static/icon_yiguanbi.png",
    title: "已关闭",
    num: 0,
    status: 4,
  },
]);

const orderData = reactive({
  allNum: 0,
  unPayNum: 0,
  unSendNum: 0,
  unFinishNum: 0,
});

const userInfo = reactive({});
const codeWx = ref();
const isShowBindPhone = ref(false);
const titleBg = ref();
const titleTextBg = ref();

const avatarUrlLocal = ref();
const nickNameLocal = ref();

function onChooseAvatar(e) {
  const res = e.detail.avatarUrl;
  avatarUrlLocal.value = res;

  uni.setStorageSync("avatar", avatarUrlLocal.value);
}

async function nickChange(e) {
  let { code } = await updateUserNickName({
    nickName: e.detail.value,
  });

  if (code == 200) {
    uni.setStorageSync("nickName", e.detail.value);
  }
}

const needAuth = ref(false);

onBeforeMount(() => {
  titleBg.value = uni.getStorageSync("backgroundColor");
  titleTextBg.value = uni.getStorageSync("color");

  nickNameLocal.value = uni.getStorageSync("nickName");
  avatarUrlLocal.value = uni.getStorageSync("avatar");

  if (!uni.getStorageSync("openId")) {
    needAuth.value = true;
  } else {
    getOrderDataFn();
  }
});

function authSuccess() {
  getOrderDataFn();
}

async function getOrderDataFn() {
  let { code, data } = await getOrderData({});
  if (code == 200) {
    Object.assign(orderData, data.dateil);

    orderStatusItems.value.forEach((item, index) => {
      switch (index) {
        case 0: // 待发货
          item.num = orderData.unSendNum;
          break;
        case 1: // 待收货
          item.num = orderData.unFinishNum;
          break;
        case 2: // 已完成
          item.num = orderData.finishNum;
          break;
        case 3: // 已关闭
          item.num = orderData.closeNum;
          break;
      }
    });
  }
}

function goOrderList(item) {
  uni.navigateTo({
    url: "/pages/order/index?status=" + item.status,
  });
}

function goAgreement(type) {
  uni.navigateTo({
    url: "/pages/agreement/index?type=" + type,
  });
}

function doAuth() {
  uni.navigateTo({
    url: "/pages/auth/index",
  });
}
</script>

<style lang="scss" scoped>
.content {
  background-color: #fff;
  height: 100vh;
}

.bk {
  width: 100vw;
  height: 150rpx;
  // background-image: url("../../static/bk_user.png");
  // background-color: #ff4671;
}

.user_info {
  width: 95vw;
  height: 200rpx;
  background-color: white;
  border-radius: 20rpx;
  box-shadow: 0 0 10rpx rgba($color: #000000, $alpha: 0.3);
  margin: 20rpx;
  z-index: 10;
  position: fixed;
  top: 40rpx;
  display: flex;
  align-items: center;
}

.bk_info {
  position: absolute;
  right: 0;
  width: 350rpx;
  height: 100%;
}

.avater {
  width: 100rpx;
  height: 100rpx;
  border-radius: 100rpx;
  // background-color: #ccc;
}

.info_data {
  display: flex;
  padding: 40rpx;
}

.info {
  height: 100rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  margin-left: 10rpx;
}

.order_all {
  display: flex;
  flex-direction: column;
  margin-top: 120rpx;
}

.order_title {
  display: flex;
  justify-content: space-between;
  height: 80rpx;
  align-items: center;
  padding: 0 50rpx;
  // border-bottom: 1px solid #f6f6f6;
  // border-top: 1px solid #f6f6f6;
}

.order_status {
  display: flex;
  font-size: 24rpx;
}

.r_status {
  display: flex;
  flex-direction: column;
  text-align: center;
  justify-content: center;
  align-items: center;
  position: relative;
}

.status_icon {
  width: 55rpx;
  height: 50rpx;
  margin-bottom: 20rpx;
}

.menu {
  display: flex;
  flex-direction: column;
  margin-top: 50rpx;
}

.menu_item {
  display: flex;
  justify-content: space-between;
  margin: 0 50rpx;
  height: 100rpx;
  border-top: 1px solid #f6f6f6;
  align-items: center;
}
.menu_item:last-child {
  border-bottom: 1px solid #f6f6f6;
}

.menu_title {
  font-size: 26rpx;
}

.modal_phone_bk {
  position: fixed;
  top: 0;
  width: 100vw;
  height: 100vh;
  background-color: rgb(0, 0, 0, 0.5);
  z-index: 9999;
}

.modal_phone {
  position: fixed;
  top: 400rpx;
  left: 100rpx;
  width: 500rpx;
  height: 250rpx;
  background-color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  justify-content: space-between;
  padding-top: 50rpx;
  padding-bottom: 30rpx;
  border-radius: 10rpx;
  z-index: 99999;
}

.modal_option {
  width: 100%;
  display: flex;
  flex-direction: row;
}

.use_btn {
  width: 70%;
  height: 50rpx;
  line-height: 50rpx;
  font-size: 24rpx;
  border-radius: 100rpx;
  background-color: #8ec3ac;
}

.r_btn {
  width: 50%;
  display: flex;
  text-align: center;
  justify-content: center;
  align-items: center;
}

.modal_title {
  display: flex;
  flex-direction: column;
}

.repair-button {
  background: none;
  border: none;
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0px;
}
.repair-button::after {
  background: none;
  border: none;
}

.tips {
  height: 50rpx;
  background-color: #ffd27e;
  color: rgb(255, 122, 78);
  font-size: 22rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 30rpx;
  padding-right: 20rpx;
}

.auth_btn {
  background-color: #000;
  color: #fff;
  border-radius: 100rpx;
  height: 30rpx;
  font-size: 20rpx;
  width: 170rpx;
  height: 35rpx;
  display: flex;
  text-align: center;
  justify-content: center;
  align-items: center;
  box-shadow: 0 0 3rpx rgba($color: #000000, $alpha: 0.1);
}
</style>
